﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FileRetrieval
{
    public static class FileUtils
    {
        public static void Director(string dir)
        {
            DirectoryInfo d = new DirectoryInfo(dir);
            FileSystemInfo[] fsinfos = d.GetFileSystemInfos();
            foreach (FileSystemInfo fsinfo in fsinfos)
            {
                if (fsinfo is DirectoryInfo)     //判断是否为文件夹
                {
                    Director(fsinfo.FullName);//递归调用
                }
                else
                {
                    Console.WriteLine(fsinfo.FullName);//输出文件的全部路径
                }
            }
        }

        // 测试一下
        public static void TestGetCurrentFileList(string dataPath, Dictionary<string, string> configs)
        {
            StringBuilder stringBuilder = new StringBuilder();
            for (var i = 0; i < configs.Count; i++)
            {
                var item = configs.ElementAt(i);
                var con1 = item.Key;
                var con2 = item.Value;
                var conArray = con2.Split(',');

                stringBuilder.AppendLine(string.Format("[{0}]", con1));
                foreach (var a in conArray)
                {
                    var fs = GetFileList(dataPath, names: a);
                    long size = 0;
                    foreach (var f in fs)
                    {
                        size += GetFileLength(f.FullName);
                        //stringBuilder.AppendLine($"文件名:{f.FullName},大小:{ GetFileSize(GetFileLength(f.FullName))}");
                    }

                    stringBuilder.AppendLine($"(扩展名:{a},数量:{fs.Count},大小:{GetFileSize(size)})");
                }
            }
            WriteData(stringBuilder.ToString());
        }

        private static long GetFileLength(string filePath)
        {
            FileInfo t = new FileInfo(filePath);//获取文件
            var size = t.Length;//这样我们就获取到了文件的大小
            return size;
        }

        /// <summary>
        /// 格式化文件大小
        /// </summary>
        /// <param name="filesize">文件传入大小</param>
        /// <returns></returns>
        private static string GetFileSize(long filesize)
        {
            try
            {
                if (filesize < 0)
                {
                    return "0";
                }
                else if (filesize >= 1024 * 1024 * 1024)  //文件大小大于或等于1024MB    
                {
                    return string.Format("{0:0.00} GB", (double)filesize / (1024 * 1024 * 1024));
                }
                else if (filesize >= 1024 * 1024) //文件大小大于或等于1024KB    
                {
                    return string.Format("{0:0.00} MB", (double)filesize / (1024 * 1024));
                }
                else if (filesize >= 1024) //文件大小大于等于1024bytes    
                {
                    return string.Format("{0:0.00} KB", (double)filesize / 1024);
                }
                else
                {
                    return string.Format("{0:0.00} bytes", filesize);
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        private static void WriteData(string str)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + "Info.txt";

            using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8))
            {
                sw.Write(str);
            }
        }


        /// <summary>
        /// 递归遍历文件夹获取文件
        /// </summary>
        /// <param name="path"></param>
        public static List<FileInfo> GetFileList(string path, bool isRecursion = true, int pathDeep = 20, List<FileInfo> fileInfos = null, params string[] names)
        {
            if (fileInfos == null)
            {
                fileInfos = new List<FileInfo>();
            }
            if (pathDeep < 0)
            {
                return fileInfos;
            }
            DirectoryInfo theFolder = new DirectoryInfo(path);
            //获取文件夹下文件
            FileInfo[] files = theFolder.GetFiles();
            foreach (var file in files)
            {
                foreach (var fn in names)
                {
                    if (string.IsNullOrEmpty(fn))
                    {
                        continue;
                    }
                    if (file.Name.ToLower() == fn.ToLower())
                    {
                        fileInfos.Add(file);
                    }
                }

            }
            if (isRecursion)
            {
                // 如果是递归，遍历该文件夹下的文件夹
                var dirs = theFolder.GetDirectories();
                if (!dirs.Any())
                {
                    return fileInfos;
                }
                foreach (var dir in dirs)
                {
                    GetFileList(dir.FullName, isRecursion, --pathDeep, fileInfos, names);
                }
            }
            return fileInfos;
        }

    }

}
